package com.demo.mail;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.sun.mail.util.MailSSLSocketFactory;

public class EmailSendUtil {
//    private final static String host = "smtp.163.com"; //腾讯企业邮的服务器
    private final static String host = "smtphz.qiye.163.com"; //腾讯企业邮的服务器
//    private final static String senderEmail = "unouno_mx@163.com";//发送人邮箱
//    private final static String senderPassword = "VUEMZYOQMKRLBDUJ"; //授权码
//    private final static String senderEmail = "sanbi_biz@163.com";//发送人邮箱
//    private final static String senderPassword = "SFWNQCRPKHRJSOHP"; //授权码
private final static String senderEmail = "service@unouno.mx";//发送人邮箱
    private final static String senderPassword = "ErwW9KNPUWTc1epW"; //授权码

    private final static String replayAddress = "unouno_mx@163.com"; //发送人邮箱
    private final static String port = "465"; //端口号
    private final static String senderSignature="UNOUNO";


    public static void sendHtmlMail(EmailInfoEntity info)throws Exception{
        info.setHost(host);
        info.setSenderEmail(senderEmail);
        info.setSenderPassword(senderPassword);   //网易邮箱的授权码~不一定是密码
        info.setReplayAddress(replayAddress);
        info.setPort(port);
        info.setSenderSignature(senderSignature);
        Message message = getMessage(info);
        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(info.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        message.setContent(mainPart);
        Transport.send(message);
    }

    public static void sendTextMail(EmailInfoEntity info) throws Exception {

        info.setHost(host);
        info.setSenderEmail(senderEmail);
        info.setSenderPassword(senderPassword);  //网易邮箱的授权码~不一定是密码
        info.setReplayAddress(replayAddress);
        info.setPort(port);
        info.setSenderSignature(senderSignature);

        Message message = getMessage(info);
        //消息发送的内容
        message.setText(info.getContent());

        Transport.send(message);
    }

    private static Message getMessage(EmailInfoEntity info) throws Exception{
        final Properties p = System.getProperties() ;
        p.setProperty("mail.smtp.host", info.getHost());
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.user", info.getSenderEmail());
        p.setProperty("mail.smtp.pass", info.getSenderPassword());
        p.setProperty("mail.smtp.port", info.getPort());//设置端口
        p.setProperty("mail.transport.protocol", "smtps");//

        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            p.put("mail.smtp.ssl.enable", "true");
            p.put("mail.smtp.ssl.socketFactory", sf);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
            String errInfo=e1.getMessage();
            System.out.println(errInfo);
        }

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session session = Session.getDefaultInstance(p, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
            }
        });
        session.setDebug(false);
        Message message = new MimeMessage(session);
        //消息发送的主题
        message.setSubject(info.getSubject());
        //接受消息的人
        //message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
        //消息的发送者
        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),info.getSenderSignature()));
        // 创建邮件的接收者地址，并设置到邮件消息中
        //单人接收邮件
        //message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
        //多人接收邮件
        message.setRecipients(Message.RecipientType.TO, info.getEmailMany());
        // 消息发送的时间
        message.setSentDate(new Date());
        return message ;
    }

    public static void main(String[] args) {
        EmailInfoEntity emailInfo=new EmailInfoEntity();
        emailInfo.setContent("test at time: "+ LocalDateTime.now());
        emailInfo.setSubject("test title");
//        emailInfo.setToAddress("wg_simple@qq.com");
        List<InternetAddress> receivers=new ArrayList();
        InternetAddress address1=new InternetAddress();
        address1.setAddress("wg_simple@qq.com");
//        address1.setPersonal("wg_simple");
        receivers.add(address1);
        InternetAddress[] addresses= receivers.toArray(new InternetAddress[receivers.size()]);

        emailInfo.setEmailMany(addresses);
        try {
            sendTextMail(emailInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




